home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / mpeg / hybrid.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  233 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /* This file contains C code to implement an ordered dither. */
  22.  
  23. #include "video.h"
  24. #include "proto.h"
  25. #include "dither.h"
  26.  
  27. #define DITH_SIZE 16
  28.  
  29.  
  30. /* Structures used to implement hybrid ordered dither/floyd-steinberg
  31.    dither algorithm.
  32. */
  33.  
  34. static unsigned char *l_darrays[DITH_SIZE];
  35. static unsigned char cr_fsarray[256][4];
  36. static unsigned char cb_fsarray[256][4];
  37.  
  38.  
  39. /*
  40.  *--------------------------------------------------------------
  41.  *
  42.  *  InitHybridDither--
  43.  *
  44.  *    Structures intialized for hybrid dithering. Ordered dither
  45.  *      patterns set for luminance channel, f-s errors precomputed
  46.  *      for chrominance channels.
  47.  *
  48.  * Results:
  49.  *    None.
  50.  *
  51.  * Side effects:
  52.  *      None.
  53.  *
  54.  *--------------------------------------------------------------
  55.  */
  56.  
  57. void
  58. InitHybridDither()
  59. {
  60.   int i, j, k, err_range, threshval;
  61.   unsigned char *lmark;
  62.  
  63.   for (i=0; i<DITH_SIZE; i++) {
  64.     lmark = l_darrays[i] = (unsigned char *) malloc(256);
  65.  
  66.     for (j=0; j<lum_values[0]; j++) {
  67.       *lmark++ = 0;
  68.     }
  69.  
  70.     for (j=0; j<(LUM_RANGE-1); j++) {
  71.       err_range = lum_values[j+1] - lum_values[j];
  72.       threshval = ((i * err_range) / DITH_SIZE)+lum_values[j];
  73.  
  74.       for (k=lum_values[j]; k<lum_values[j+1]; k++) {
  75.     if (k > threshval) *lmark++ = ((j+1) * (CR_RANGE * CB_RANGE));
  76.     else *lmark++ = (j * (CR_RANGE * CB_RANGE));
  77.       }
  78.     }
  79.  
  80.     for (j=lum_values[LUM_RANGE-1]; j<256; j++) {
  81.       *lmark++ = (LUM_RANGE-1)*(CR_RANGE * CB_RANGE);
  82.     }
  83.  
  84.   }
  85.   {
  86.     int cr1, cr2, cr3, cr4, err1, err2;
  87.     int cb1, cb2, cb3, cb4, val, nval;
  88.  
  89.     for (i=0; i<256; i++) {
  90.  
  91.       val = i;
  92.  
  93.       cr1 = (val * CR_RANGE) / 256;
  94.       err1 = (val - cr_values[cr1])/2;
  95.       err2 = (val - cr_values[cr1]) - err1;
  96.  
  97.       nval = val+err1;
  98.       if (nval > 255) nval = 255;
  99.       else if (nval < 0) nval = 0;
  100.       cr2 = (nval * CR_RANGE) / 256;
  101.       err1 = (nval - cr_values[cr2])/2;
  102.  
  103.       nval = val+err2;
  104.       if (nval > 255) nval = 255;
  105.       else if (nval < 0) nval = 0;
  106.       cr3 = (nval * CR_RANGE) / 256;
  107.       err2 = (nval - cr_values[cr3])/2;
  108.  
  109.       nval = val+err1+err2;
  110.       if (nval > 255) nval = 255;
  111.       else if (nval < 0) nval = 0;
  112.       cr4 = (nval * CR_RANGE) / 256;
  113.  
  114.       cr_fsarray[i][0] = cr1*CB_RANGE;
  115.       cr_fsarray[i][1] = cr2*CB_RANGE;
  116.       cr_fsarray[i][2] = cr3*CB_RANGE;
  117.       cr_fsarray[i][3] = cr4*CB_RANGE;
  118.     }
  119.     
  120.     for (i=0; i<256; i++) {
  121.  
  122.       val = i;
  123.  
  124.       cb1 = (val * CB_RANGE) / 256;
  125.       err1 = (val - cb_values[cb1])/2;
  126.       err2 = (val - cb_values[cb1]) - err1;
  127.  
  128.       nval = val+err1;
  129.       if (nval > 255) nval = 255;
  130.       else if (nval < 0) nval = 0;
  131.       cb2 = (nval * CB_RANGE) / 256;
  132.       err1 = (nval - cb_values[cb2])/2;
  133.  
  134.       nval = val+err2;
  135.       if (nval > 255) nval = 255;
  136.       else if (nval < 0) nval = 0;
  137.       cb3 = (nval * CB_RANGE) / 256;
  138.       err2 = (nval - cb_values[cb3])/2;
  139.  
  140.       nval = val+err1+err2;
  141.       if (nval > 255) nval = 255;
  142.       else if (nval < 0) nval = 0;
  143.       cb4 = (nval * CB_RANGE) / 256;
  144.  
  145.       cb_fsarray[i][0] = cb1;
  146.       cb_fsarray[i][1] = cb2;
  147.       cb_fsarray[i][2] = cb3;
  148.       cb_fsarray[i][3] = cb4;
  149.     }
  150.   }
  151. }
  152.  
  153. /*
  154.  *--------------------------------------------------------------
  155.  *
  156.  * HybridDitherImage --
  157.  *
  158.  *    Dithers an image using an ordered dither.
  159.  *    Assumptions made:
  160.  *      1) The color space is allocated y:cr:cb = 8:4:4
  161.  *      2) The spatial resolution of y:cr:cb is 4:1:1
  162.  *      The luminance channel is dithered based on the standard
  163.  *      ordered dither pattern for a 4x4 area. The Chrominance 
  164.  *      channels are dithered based on precomputed f-s errors.
  165.  *      Two errors are propogated per pixel. Errors are NOT propogated
  166.  *      beyond a 2x2 pixel area. 
  167.  *
  168.  * Results:
  169.  *    None.
  170.  *
  171.  * Side effects:
  172.  *    None.
  173.  *
  174.  *--------------------------------------------------------------
  175.  */
  176. void
  177. HybridDitherImage (lum, cr, cb, out, h, w)
  178.     unsigned char *lum;
  179.     unsigned char *cr;
  180.     unsigned char *cb;
  181.     unsigned char *out;
  182.     int w, h;
  183. {
  184.   unsigned char *l, *r, *b, *o1, *o2;
  185.   unsigned char *l2;
  186.   int i, j;
  187.  
  188.   l = lum;
  189.   l2 = lum+w;
  190.   r = cr;
  191.   b = cb;
  192.   o1 = out;
  193.   o2 = out+w;
  194.  
  195.   for (i=0; i<h; i+=4) {
  196.  
  197.     for (j=0; j<w; j+=4) {
  198.  
  199.       *o1++ = pixel[(l_darrays[0][*l++] | cr_fsarray[*r][0] | cb_fsarray[*b][0])];
  200.       *o1++ = pixel[(l_darrays[8][*l++] | cr_fsarray[*r][1] | cb_fsarray[*b][1])];
  201.       *o2++ = pixel[(l_darrays[12][*l2++] | cr_fsarray[*r][2] | cb_fsarray[*b][2])];
  202.       *o2++ = pixel[(l_darrays[4][*l2++] | cr_fsarray[*r++][3] | cb_fsarray[*b++][3])];
  203.  
  204.       *o1++ = pixel[(l_darrays[2][*l++] | cr_fsarray[*r][0] | cb_fsarray[*b][0])];
  205.       *o1++ = pixel[(l_darrays[10][*l++] | cr_fsarray[*r][1] | cb_fsarray[*b][1])];
  206.       *o2++ = pixel[(l_darrays[14][*l2++] | cr_fsarray[*r][2] | cb_fsarray[*b][2])];
  207.       *o2++ = pixel[(l_darrays[6][*l2++] | cr_fsarray[*r++][3] | cb_fsarray[*b++][3])];
  208.     }
  209.  
  210.     l += w; l2 += w;
  211.     o1 += w; o2 += w;
  212.  
  213.     for (j=0; j<w; j+=4) {
  214.  
  215.       *o1++ = pixel[(l_darrays[3][*l++] | cr_fsarray[*r][1] | cb_fsarray[*b][1])];
  216.       *o1++ = pixel[(l_darrays[11][*l++] | cr_fsarray[*r][0] | cb_fsarray[*b][0])];
  217.       *o2++ = pixel[(l_darrays[15][*l2++] | cr_fsarray[*r][3] | cb_fsarray[*b][3])];
  218.       *o2++ = pixel[(l_darrays[7][*l2++] | cr_fsarray[*r++][2] | cb_fsarray[*b++][2])];
  219.  
  220.       *o1++ = pixel[(l_darrays[1][*l++] | cr_fsarray[*r][1] | cb_fsarray[*b][1])];
  221.       *o1++ = pixel[(l_darrays[9][*l++] | cr_fsarray[*r][0] | cb_fsarray[*b][0])];
  222.       *o2++ = pixel[(l_darrays[13][*l2++] | cr_fsarray[*r][3] | cb_fsarray[*b][3])];
  223.       *o2++ = pixel[(l_darrays[5][*l2++] | cr_fsarray[*r++][2] | cb_fsarray[*b++][2])];
  224.     }
  225.  
  226.     l += w; l2 += w;
  227.     o1 += w; o2 += w;
  228.   }
  229. }
  230.  
  231.  
  232.   
  233.